package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @ApiOperation("角色分页查询")
    @PostMapping("/page/{pageNum}/{pageSize}")
      public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(@RequestBody RoleDto roleDto,
    @PathVariable Integer pageNum ,@PathVariable Integer pageSize ){
        PageResponse <RoleVo> roleVoPage=roleService.findRoleVoPage(roleDto,pageNum,pageSize);
             return ResponseResult.success(roleVoPage);
      }
      //角色添加
   @PutMapping
    @ApiOperation("角色添加")
    public ResponseResult<Boolean> createRole( @RequestBody RoleDto roleDto){

          roleService.createRole(roleDto);

        return ResponseResult.success();

    }
    //角色修改
    @PatchMapping
    @ApiOperation("角色的修改")
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto){
        Boolean flag=roleService.updateRole(roleDto);
        return  ResponseResult.success(flag);
    }
    //删除角色
    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public ResponseResult delectRole(@PathVariable("roleId") Long roleId) {
        return ResponseResult.success(roleService.deleteRoleById(roleId));
    }
    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult<Set<String>> findCheckedResources( @PathVariable("roleId") Long roleId){
        Set<String> set = roleService.findCheckedResources(roleId);
        return ResponseResult.success(set);
    }
    @PostMapping("/init-roles")
    @ApiOperation("角色的下拉框")
    public ResponseResult<List<RoleVo>> initRoles(){
                List<RoleVo> list=roleService.initRoles();
                     return ResponseResult.success(list);
   }
}
